<?php
session_start();

/*
 * Inserts a new page into the databse;
 *
 */

// Return json encoded data
header ('Content-type: application/json');

require_once '../core/db.php';

if(isset($_SESSION['user'])) {			// If user is logged on, go ahead:
    
	$sql = 'INSERT INTO sitePages (pageName, userId, parent, css, private) VALUES (?, ?, ?, ?, ?)';
    $sth = $db->prepare ($sql);
    $res = $sth->execute (array ($_POST['name'], $_SESSION['user'], $_POST['parent'], $_POST['css'], $_POST['privat']));
	$newId = $db->lastInsertId();
    if ($res == 1) {
		echo json_encode (array ('ok'=>'OK', 'message' => 'Ny side ble opprettet.', 'id' => $newId));
	}
    else
    	echo json_encode (array ('message'=>'Kunne ikke lagre side i databasen.'));
}
else {
    echo json_encode (array ('message'=>'Du kan ikke opprette en side uten å være logget inn.'));
}
?>